import {createStore} from "vuex";
import {App} from "vue";
import {getAdminInfoApi} from "../request/api";

interface State {
    menus: {
        parentId: number;
        id: number
    } [],
    adminInfo: AdminInfoItf
}

interface MenuObj {
    parentId: number;
    id: number;
    children?: MenuObj[]
}

interface NewMenus {
    [key: number]: MenuObj
}

const store = createStore<State>({
    state() {
        return {
            menus: [],
            adminInfo: {
                menus: []
            }
        }
    },
    getters: {
        getAdminInfo(state) {
            const adminInfo: AdminInfoItf = {
                icon: state.adminInfo.icon,
                roles: state.adminInfo.roles,
                username: state.adminInfo.username,
                menus: handleMenus(state.adminInfo.menus)
            }
            return adminInfo
        },

    },
    mutations: {
        /*updateMenus(state, menus) {
            state.menus = menus
        },*/
        updateAdminInfo(state, adminInfo) {
            state.adminInfo = adminInfo
        }
    },
    actions: {
        getAdminInfo({commit}) {
            return new Promise((resolve, reject) => {
                getAdminInfoApi().then(res => {
                    if (res.code === 200) {
                        console.log(res)
                        commit('updateAdminInfo', res.data);
                        resolve(res.data);
                    } else {
                        reject(res);
                    }
                })
            })
        }
    },
    modules: {}
});

const handleMenus = (menus: MenuListItf[]): MenuListItf[] => {
    const newMenus: MenuListItf[] = []
    for (let i = 0; i < menus.length; i++) {
        if (menus[i].parentId === 0) {
            // 一级菜单
            newMenus[menus[i].id as number] = {...menus[i], children: newMenus[menus[i].id as number]?.children || []} // 浅拷贝
        } else {
            // 二级菜单
            let parentId = menus[i].parentId as number; // 对应的一级菜单id
            newMenus[parentId] = newMenus[parentId] || {};
            newMenus[parentId].children = newMenus[parentId].children || [];
            newMenus[parentId].children?.push(menus[i])
        }
    }
    // 过滤空元素
    return newMenus.filter(item => Object.keys(item).length > 0);

    /*const newMenus: NewMenus = {};
    // 获取旧的菜单
    const menus = state.adminInfo.menus;
    for (let i = 0; i < menus.length; i++) {
        if (menus[i].parentId === 0) {
            // 一级菜单
            newMenus[menus[i].id] = {...menus[i], children: newMenus[menus[i].id]?.children || []} // 浅拷贝
        } else {
            // 二级菜单
            let parentId = menus[i].parentId; // 对应的一级菜单id
            newMenus[parentId] = newMenus[parentId] || {};
            newMenus[parentId].children = newMenus[parentId].children || [];
            newMenus[parentId].children?.push(menus[i])
        }
    }*/
}

export const initStore = (app: App<Element>) => {
    app.use(store);
}

export default store;
